<?php

namespace app\middleware;

use app\admin\model\AdminAdmin;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class AdminCheck implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
        //无需验证码控制器数组
        $arr = [
            'admin' => [
                'app\admin\controller\Login' => [
                    'verify',
                    'index',
                ],
            ],
        ];

        //验证登录
        if ((new AdminAdmin)->isLogin() == false) {
            if (empty($arr[$request->app][$request->controller])) {
                return redirect(APP_ADMIN.'/login/index');
            } else {
                if (!in_array($request->action, $arr[$request->app][$request->controller])) {
                    return redirect(APP_ADMIN.'/login/index');
                }
            }
        }
        return $next($request);
    }
}

